from common.models import MyImageModel, UserOwnedModel, DateAwareModel
from django.contrib.auth.models import User
from django.db import models
from django.db.models.signals import post_save
from reviews.models import Dish
from sorting.models import List

class UserProfile(DateAwareModel):
    name = models.CharField(max_length=255)
    bookmarks = models.ForeignKey(Dish, blank=True, null=True)	
    lists = models.ManyToManyField(List, blank=True, null=True)
    location = models.CharField(max_length=255, blank=True)
    user = models.OneToOneField(User)
    website = models.URLField(blank=True, verify_exists=False)

    def __unicode__(self):
        return str(self.user)

class ProfileImage(MyImageModel):
    profile = models.ForeignKey(UserProfile)

    class IKOptions:
    # This inner class is where we define the ImageKit options for the model
        spec_module = 'kokoomi.profile.specs'
        cache_dir = 'images'
        image_field = 'image'
        save_count_as = 'num_views'

class Connection(DateAwareModel):
    follower = models.ForeignKey(UserProfile, related_name='following')
    leader = models.ForeignKey(UserProfile, related_name='followers')

def create_profile(sender, **kw):
    user = kw["instance"]
    if kw["created"]:
        profile = UserProfile(user=user)
        profile.save()

post_save.connect(create_profile, sender=User, dispatch_uid="users-profilecreation-signal")